//
//  ViewController.m
//  People
//
//  Created by 曾泽昱 on 2024/7/11.
//

/*
 关于KVC valueForkey：key 的调用
    1.先调用相关方法
        getter方法-getKey->key-〉isKey
        
    2.如果没有相关方法+（Bool）accessInstanceVariablesDirectly 返回值
    如果是YES的话，先后顺序是：_key->_isKey->key->isKey
    如果是No的话，就会抛异常
 */

#import "ViewController.h"
#import "People.h"
@interface ViewController ()

@property (nonatomic,strong) People* p;

@end

@implementation ViewController

//-(void)viewDidLoad{
//    [super viewDidLoad];
//    People* p = [[People alloc] init];
//    NSString* name = [p valueForKey:@"name"];
//    NSLog(@"%@",name);
//}

-(void)viewDidLoad{
    [super viewDidLoad];
    People* person = [[People alloc] init];
    person.name = @"花无缺";
    self.p = person;
    [person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
    person.name = @"小鱼儿";
    int i = 1;
    person.name = @"龙湾";
    int z = 1;
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString*,id> *)change context:(void *)context{
    NSLog(@"%@",change);
}

-(void)dealloc{
    [self.p removeObserver:self forKeyPath:@"name"];
}

@end
